<template>
  <div class="app-container">
    <div v-if="showSearch" class="search-container">
      <CommonSearch
        :formConfig="searchColumns"
        :inline="true"
        labelWidth="80px"
        formItemWidth="auto"
        :labelPosition="'left'"
        :model="searchParam"
      >
        <template #after>
          <el-button type="primary" @click="search">
            <i-ep-search />
            搜索
          </el-button>
          <el-button @click="handleReset">
            <i-ep-refresh />
            重置
          </el-button>
        </template>
      </CommonSearch>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button
          @click="
            useDownload(
              '/andon/andcall/callInformExport',
              '通知信息查询',
              searchParam
            )
          "
        >
          <template #icon><i-ep-download /></template>导出</el-button
        >
      </template>
      <!-- 表格主体 -->
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        v-bind="$attrs"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
        @sort-change="onTableSortChanged"
      >
        <!-- <el-table-column type="selection" width="55" align="center" /> -->

        <!-- 表格内容操作 -->
        <template v-for="item in tableColumns" :key="item">
          <TableColumn v-if="item.prop && item.isShow" :column="item">
            <template v-for="slot in Object.keys($slots)" #[slot]="scope">
              <slot :name="slot" v-bind="scope"></slot>
            </template>
          </TableColumn>
        </template>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <!-- 列设置 抽屉组件 -->
    <ColSetting
      v-model:col-setting="tableColumns"
      v-model:search-setting="searchColumns"
      @search-handle="(e:boolean) => (showSearch = e)"
    />
  </div>
</template>

<script setup lang="tsx">
import { useTable, useDictionary, useDownload } from "@/utils/useTable";
import { getPlantList, getWorkShopList } from "@/utils/commonData";
import { getAndstopreasonList, getCrewEquipmentList } from "@/api/andon/andon";

defineOptions({
  name: "ANDONANDNORICEQUERY",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/andon/andcall/selectByPage";

// 是否需要分页
const pagination = true;

const initParam = reactive({ sort: {} });

// 初始化搜索参数数据
const searchParamInit = {
  plantId: "",
  workCenterId: "",
  equipmentId: "",
  callMemberName: "",
  stopReasonId: "",
  callTimeSearch: [],
  callTimeStart: "",
  callTimeEnd: "",
};

const deleteParam = {
  api: "/system/sysRole/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

// 数据请求错误后处理
// function dataCallBack() {}   //需要处理
const dataCallBack = ref(undefined); //无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

// 监听表格排序方法
const onTableSortChanged = (column: any) => {};

const tableColumns = reactive([
  { prop: "plantCode", label: "工厂", minWidth: 150, isShow: true },
  { prop: "callCode", label: "呼叫编号", minWidth: 200, isShow: true },
  { prop: "callMemberName", label: "呼叫人", minWidth: 120, isShow: true },
  {
    prop: "workCenterName",
    label: "工作中心",
    minWidth: 200,
    isShow: true,
    render: (scope: any) => (
      <el-text>{`${scope.row.workCenterCode} - ${scope.row.workCenterName}`}</el-text>
    ),
  },
  { prop: "equipmentName", label: "设备", minWidth: 150, isShow: true },
  { prop: "crewCode", label: "响应班组", minWidth: 150, isShow: true },
  // { prop: "id", label: "维修通知人", minWidth: 120, isShow: true },
  // { prop: "id", label: "质量通知人", minWidth: 120, isShow: true },
  { prop: "stopName", label: "停机类型", minWidth: 150, isShow: true },
  { prop: "stopReasonName", label: "停机原因", minWidth: 150, isShow: true },
  { prop: "callTime", label: "呼叫时间", minWidth: 180, isShow: true },
  { prop: "answerTime", label: "响应时间", minWidth: 180, isShow: true },
  { prop: "arriveTime", label: "到场时间", minWidth: 180, isShow: true },
  { prop: "issueTime", label: "解决时间", minWidth: 180, isShow: true },
  { prop: "callState", label: "呼叫状态", minWidth: 120, isShow: true },
  {
    prop: "totalTime",
    label: "处理时长",
    minWidth: 120,
    isShow: true,
    align: "right",
  },
  { prop: "processorName", label: "处理人", minWidth: 120, isShow: true },
]);

// 是否显示搜索框
const showSearch = ref(true);
// 搜索参数 设置为响应式
const searchColumns: any = reactive([
  {
    type: "select",
    label: "工厂",
    isShow: true,
    fieldName: "plantId",
    placeholder: "请选择工厂",
    initValue: null,
    defaultFirstOption: true,
    optionList: [],
    change: (e) => {
      selectPlant(e);
    },
  },
  {
    type: "select",
    label: "工作中心",
    isShow: true,
    fieldName: "workCenterId",
    placeholder: "请选择工作中心",
    initValue: null,
    defaultFirstOption: true,
    optionList: [],
  },
  {
    type: "select",
    label: "设备",
    isShow: true,
    fieldName: "equipmentId",
    placeholder: "请选择设备",
    initValue: null,
    defaultFirstOption: true,
    optionList: [],
  },
  {
    type: "input",
    label: "呼叫人",
    isShow: true,
    fieldName: "callMemberName",
    placeholder: "请选择呼叫人",
    initValue: "",
    defaultFirstOption: true,
  },
  {
    type: "select",
    label: "停机原因",
    isShow: true,
    fieldName: "stopReasonId",
    placeholder: "请选择停机原因",
    initValue: null,
    defaultFirstOption: true,
    optionList: [],
  },
  {
    type: "date",
    dataType: "daterange",
    label: "呼叫时间",
    isShow: true,
    fieldName: "callTimeSearch",
    startPlaceholder: "请选择起始呼叫时间",
    endPlaceholder: "请选择截止呼叫时间",
    initValue: null,
    valueFormat: "YYYY-MM-DD",
    defaultFirstOption: true,
    change: (val) => {
      if (val) {
        searchParam.value.callTimeStart = val[0];
        searchParam.value.callTimeEnd = val[1];
      } else {
        searchParam.value.callTimeStart = "";
        searchParam.value.callTimeEnd = "";
      }
    },
  },
]);

// 获取工厂列表
const getSelectPlantList = async () => {
  const list = (await getPlantList()) ?? [];
  list?.map((item: any) => {
    item.label = `${item.organizationCode}`;
    item.value = `${item.id}`;
  });
  searchColumns[0].optionList = list;
};

// 工厂选择
const selectPlant = async (e: string) => {
  if (e) {
    // 获取工作中心列表
    const list = (await getWorkShopList(e)) ?? [];
    list?.map((item: any) => {
      item.label = `${item.workshopCode}-${item.workshopName}`;
      item.value = `${item.id}`;
    });
    searchColumns[1].optionList = list;
    searchParam.value.workCenterId = "";
  } else {
    searchColumns[1].optionList = [];
    searchParam.value.workCenterId = "";
  }
};

// 获取停机原因
const getStopReasonList = async () => {
  const { data } = await getAndstopreasonList();
  searchColumns[4].optionList = data?.map((item: any) => ({
    label: item.stopReasonName,
    value: item.id,
  }));
};

// 获取设备列表
const getEquipmentNameList = async () => {
  const { data } = await getCrewEquipmentList();
  searchColumns[2].optionList = data?.map((item: any) => ({
    label: item.equipmentName,
    value: item.id,
  }));
};

// 重置数据处理
function handleReset() {
  reset();
  getSelectPlantList();
  getStopReasonList();
  getEquipmentNameList();
  searchColumns[1].optionList = [];
}

onMounted(async () => {
  await Promise.all([
    getTableList(),
    getSelectPlantList(),
    getStopReasonList(),
    getEquipmentNameList(),
  ]);
});
</script>

<style scoped lang="scss"></style>
